/*****************************
 *@file: BaseHeroCharacter
 *@author: 陈吕唐
 *@desc: 英雄基础类
 *@date: 2024-03-19	11:14
 *****************************/
import { CircleCollider2D, _decorator, error, sp } from 'cc';
import SkillVO from '../../pojo/vo/SkillVO';
import { BaseCharacter } from '../BaseCharacter';
import { BaseCharacterUI } from '../BaseCharacterUI';
import { GameEnum } from '../GameEnum';
import { ActiveSkill } from '../skill/ActiveSkill';
import { BaseSkill } from '../skill/BaseSkill';
import { HeroNormalAttack } from '../skill/HeroNormalAttack';
import { PassiveSkill } from '../skill/PassiveSkill';
import IHeroAttribute from './IHeroAttribute';
const { ccclass, property } = _decorator;

@ccclass('BaseHeroCharacter')
export class BaseHeroCharacter extends BaseCharacterUI implements IHeroAttribute {
    /****************************************************************************************************************
     *Readonly 常量
     ****************************************************************************************************************/
    /****************************************************************************************************************
     *Property 定义变量
     ****************************************************************************************************************/
    private _scope: number = 0;

    public skills: SkillVO[];
    /***************************************************************
     *PropertyUI 引用的控件
     ***************************************************************/
    @property({ group: { name: 'BOX2D', id: '1' }, type: CircleCollider2D, override: true, tooltip: `当前感应范围形状(默认从当前节点查找)` })
    protected sensorCollider: CircleCollider2D = null;

    @property({ group: { name: '基础英雄视图', id: '3' }, type: sp.Skeleton, tooltip: `播放的骨骼动画` })
    protected spine: sp.Skeleton = undefined!;
    /****************************************************************************************************************
     * Lifecycle 生命周期相关方法
     ****************************************************************************************************************/
    protected onEnable(): void {
        super.onEnable();
        this.updateScope();
    }
    /****************************************************************************************************************
     * Public 公有方法
     ****************************************************************************************************************/
    /****************************************************************************************************************
     * private 私有方法
     ****************************************************************************************************************/
    /*********************************************************
     * 
     *********************************************************/
    public override init(value: IHeroAttribute): void {
        super.init(value);
        this._scope = value.scope;
        this.skills = value.skills;
        this.updateSkill(value.skills);
    }

    public override startGame(): void {
        super.startGame();
        this.spine.setAnimation(0, `idle`, true);
        this.updateScope();
    }


    /**
     * 进入感应范围
     * @param character 
     */
    protected override onSensor(character: BaseCharacter): void {
        if (character.gameIng) {
            this.enemySet.add(character);
        }
    }
    /*********************************************************
     * 
     *********************************************************/
    /**
     * 更新英雄攻击范围
     * @param value 
     * @returns 
     */
    private updateScope(value: number = this.scope) {
        if (!this.enabledInHierarchy) {
            return;
        }

        this.sensorCollider.radius = value;
    }

    /**
     * 更新技能
     * @param value 技能数据
     */
    private updateSkill(values: SkillVO[]) {

        /**
         * 技能类型
         */
        let types: GameEnum.SkillType[] =
            [
                GameEnum.SkillType.NORMAL,
                GameEnum.SkillType.PASSIVE,
                GameEnum.SkillType.ACTIVE
            ]

        for (let i = 0; i < types.length; i++) {
            const element = types[i];
            let skillItem: BaseSkill = undefined!;
            let value: SkillVO = undefined!;
            switch (element) {
                case GameEnum.SkillType.NORMAL:
                    skillItem = this.node.getComponent(HeroNormalAttack);
                    value = values.find((item) => item.type === GameEnum.SkillType.NORMAL);
                    break;
                case GameEnum.SkillType.PASSIVE:
                    skillItem = this.node.getComponent(PassiveSkill);
                    value = values.find((item) => item.type === GameEnum.SkillType.PASSIVE);
                    break;
                case GameEnum.SkillType.ACTIVE:
                    skillItem = this.node.getComponent(ActiveSkill);
                    value = values.find((item) => item.type === GameEnum.SkillType.ACTIVE);
                    break;
                default:
                    error(`未定义的技能类型类型`)
                    break;
            }

            if (!skillItem) {
                continue;
            }

            if (!value) {
                skillItem.enabled = false;
                continue;
            }

            skillItem.enabled = true;
            skillItem.updateValue(value);
        }
    }
    /****************************************************************************************************************
     * Property Get && Set 属性的 get && set 函数
     ****************************************************************************************************************/
    public get scope(): number {
        return this._scope;
    }

    public set scope(value: number) {
        if (this._scope === value) {
            return;
        }
        this._scope = value;
        this.updateScope(value);
    }
}


